home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / ThreadStuff / ThreadClasses.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  4.1 KB  |  162 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ThreadClasses.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __THREADCLASSES__
  15. #define __THREADCLASSES__
  16.  
  17. #ifndef __THREADS__
  18. #include "Threads.h"
  19. #endif
  20.  
  21. #ifndef __FAILURE__
  22. #include "Failure.h"
  23. #endif
  24.  
  25. #ifndef __TIMER__
  26. #include <Timer.h>
  27. #endif
  28.  
  29. /***********************************|****************************************/
  30.  
  31. typedef unsigned long (*FNUL)( unsigned long );
  32. typedef    unsigned long (*FNULUL)( unsigned long, unsigned long );
  33.  
  34. /***********************************|****************************************/
  35.  
  36. class TThread 
  37. {
  38.     public:
  39.                             TThread ( );
  40.     virtual                    ~TThread ();
  41.         
  42.     virtual    void            Sleep ( unsigned long howLongToSleep = 0 ) = 0;
  43.     virtual void            Yield ( ) = 0;
  44.     virtual void            Wake ( ) = 0;
  45.  
  46.     virtual OSType            GetDescriptor() = 0;
  47.     
  48.     virtual void            BeginCritical ( ) = 0;
  49.     virtual void            EndCritical ( ) = 0;
  50.  
  51.     virtual void *            GetExceptionChain ( ) const = 0;
  52.     virtual void            SetExceptionChain ( void * ) = 0;
  53.     
  54.     
  55.     // Routines to manipulate the threads.
  56.     static TThread *        GetCurrentThread ();
  57.     static void                YieldCurrentThread ( TThread* threadToYieldTo = nil );
  58.     
  59.     static unsigned long    GetCurrentThreadStackSpace ( );
  60.     
  61.     static unsigned long    GetThreadCount ( );
  62.     static TThread *        GetThread ( unsigned long index );                                                
  63.  
  64. #if debug
  65.     virtual ostream&        operator << ( ostream& s );
  66. #endif
  67.  
  68.     protected:
  69.     TThread    *                fNext;        //    for the list of all threads.
  70.     static TThread *        gCurrentThread;
  71.     static TThread *        gThreadList;
  72. };
  73.  
  74. /***********************************|****************************************/
  75.  
  76. class TThreadStatusWindow;
  77.  
  78. /***********************************|****************************************/
  79.  
  80. class TThreadManagerThread : public TThread
  81. {
  82.     public:                    TThreadManagerThread ( FNULUL proc, unsigned long param1, unsigned long param2, OSType descriptor );
  83.     protected:                TThreadManagerThread ( OSType descriptor );
  84.     public:    virtual            ~TThreadManagerThread ();    
  85.         
  86.     virtual    void            Sleep ( unsigned long howLongToSleep = 0 );
  87.     virtual void            Yield ( );
  88.     virtual void            Wake ( );
  89.  
  90.     virtual OSType            GetDescriptor();
  91.     
  92.     virtual void            BeginCritical ( );
  93.     virtual void            EndCritical ( );
  94.  
  95.     virtual void *            GetExceptionChain ( ) const;
  96.     virtual void            SetExceptionChain ( void * );
  97.  
  98.     static void                WakeUpSleepingThreadsTask ( );
  99.     
  100. #if debug
  101.     virtual ostream&        operator << ( ostream& s );
  102.     friend class TThreadStatusWindow;
  103. #endif
  104.  
  105.     protected:
  106.     //    These get called by the Thread Manager as this thread gets swapped in and out.
  107.     static pascal void         ThreadManagerSwitchInProc ( ThreadID threadBeingSwitched, void* switchProcParam );
  108.     static pascal void         ThreadManagerSwitchOutProc ( ThreadID threadBeingSwitched, void* switchProcParam );
  109.     static pascal void         TThreadManagerThreadTerminationProc ( ThreadID threadID, void * terminationProcParam );
  110.     
  111.     static pascal void        WakeUpOutstandingParamBlockCalls ( ) ;
  112.     static pascal void        WakeUpSleepingThreadTimeManagerCallback ( );
  113.     
  114.     friend pascal unsigned long TThreadInstantiationWrapper( TThreadManagerThread* thread);
  115.  
  116.         
  117.     FNULUL                    fThreadEntry;
  118.     unsigned long            fParam1;
  119.     unsigned long            fParam2;
  120.     
  121.     ThreadID                fThreadID;    
  122.     
  123.     OSType                    fDescriptor;
  124.     void *                    fExceptionChainTop;    
  125.     long                    fA5;                                                
  126.     
  127.     unsigned long            fWakeThreadAfter;
  128.     TMTask                    fWakeUpTimeManagerTask;
  129.     TMTask                    fThreadExecutionTimeManagerTask;
  130.     
  131.     const void *            fOutstandingParamBlock;
  132.     
  133.     ThreadTaskRef*            fThreadTaskRef;
  134. };
  135.  
  136. /***********************************|****************************************
  137.  *
  138.  * Macros to make switching from Herman's thread stuff to the new thread
  139.  * class.
  140.  *
  141.  ***********************************|****************************************/
  142.  
  143.  #define newThreads 1
  144.  
  145.  #define TNormalMode    0
  146.  #define TVMSafeMode    1
  147.  
  148. void TSleep ( unsigned long x );
  149. void TYield();
  150.             
  151. void TSetRefCon(OSType x);
  152. OSType TGetRefCon();
  153.  
  154. TThread*    createThread(OSType name, 
  155.                     void* proc, unsigned long stkSize, 
  156.                     unsigned long p1, unsigned long p2, 
  157.                     short execMode);
  158.                                                                                                              
  159.  # define thread TThread
  160.  
  161. #endif
  162.